/**
 * 
 */
package com.evon.yardmanagement.service;

import java.util.Collections;
import java.util.List;

import com.evon.yardmanagement.dao.OemDAO;
import com.evon.yardmanagement.model.Oem;
import com.evon.yardmanagement.security.YardSecurityManager;
import com.evon.yardmanagement.transformer.BaseTransformer;
import com.evon.yardmanagement.ui.dto.OemDTO;

/**
 * @author Nikunj
 * 
 */
public class OEMService {

	private static OemDAO oemDao = new OemDAO();
	static BaseTransformer<Oem, OemDTO> transformer = new BaseTransformer<Oem, OemDTO>(
			Oem.class, OemDTO.class);

	/*
	 * (non-Javadoc)
	 * 
	 * @seecom.evon.yardmanagement.service.IUtilityService#addOem(com.evon.
	 * yardmanagement.model.Oem)
	 */
	public static boolean addOem(OemDTO oemDTO) {
		Oem oem = transformer.transformToModel(oemDTO);
		if (oem.getId() > 0) {
			oemDao.update(oem);
			
		} else
		{
			//If transient objects , then set the vendor id
			//TODO : By pass this for super admin.
			oem.setVendor(YardSecurityManager.getCurrentVendor());
			oemDao.saveOrUpdate(oem);
		}
		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.evon.yardmanagement.service.IUtilityService#getAllOem()
	 */
	public static List<OemDTO> getAllOem() {
		// TODO Auto-generated method stub
		List<Oem> oemModelList = oemDao.findAll();
		return transformer.transformToDTO(oemModelList);
	}

	/**
	 * This method returns all the OEM belonging to the yard provided. If the
	 * yard is OEM yard, then only one OEM will be returned but if the yard is
	 * port, then all OEM served at that yard will be returned.
	 * 
	 * @param yardId
	 * @return
	 */
	public List<Oem> getOemForYard(int yardId) {
		return oemDao.getYards(yardId);
	}

	public static OemDTO getOemDetails(int id) {
		return transformer.transformToDTO((Oem) oemDao.findById(id));
	}

	public static List<OemDTO> getOems(int selectedVendorId) {
		List<Oem> oemList = oemDao.getOemsForVendor(selectedVendorId);
		if(oemList == null || oemList.isEmpty()){
			return Collections.EMPTY_LIST;
		}
		return transformer.transformToDTO(oemList);
	}

}
